package com.kevin.servlet;

import com.kevin.ejb3.HelloWorld;
import com.kevin.ejb3.HiWorld;
import com.kevin.ejb3.Injection;
import com.kevin.ejb3.LifeCycle;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by loli on 2015/3/15.
 */
public class EjbTestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            InitialContext context = new InitialContext();
            HelloWorld hello = (HelloWorld)context.lookup("HelloWorldBean/remote-com.kevin.ejb3.HelloWorld");
            HiWorld hi = (HiWorld)context.lookup("HelloWorldBean/remote-com.kevin.ejb3.HiWorld");
            hello.sayHello("kevin");
            hi.sayHi("kevin");

            LifeCycle lifeCycle = (LifeCycle) context.lookup("LifeCycleBean/remote");
            lifeCycle.say();

            Injection injection = (Injection) context.lookup("InjectionBean/remote");
            injection.SayHello();

        } catch (NamingException e) {
            e.printStackTrace();
        }

    }
}
